using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Emlyn.Website.Articles;
using FreeTextBoxControls;

namespace Emlyn.Website.ArticlesPresentation
{
    public class PresentArticleDetail : PresentArticle
    {
        protected TextBox _edArticleName;
        protected TextBox _edArticleCode;
        protected FreeTextBox _edArticleDescription;
        protected TextBox _edArticleUrl;
        private string _navigateUrl;

        public string NavigateUrl
        {
            get { return _navigateUrl; }
            set { _navigateUrl = value; }
        }

        public void OnUpdate(Object o, System.EventArgs ea)
        {
            aArticle.ArticleCode = _edArticleCode.Text;
            aArticle.ArticleName = _edArticleName.Text;
            aArticle.ArticleContent = _edArticleDescription.Text;
            aArticle.Url = _edArticleUrl.Text;
            aArticle.LastChanged = DateTime.Now;

            ArticleMod lam = new ArticleMod();
            lam.Save(aArticle);
        }

        public void OnDelete(Object o, System.EventArgs ea)
        {
            ArticleMod lam = new ArticleMod();
            lam.Delete(aArticle);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        //protected override void RenderContents(HtmlTextWriter output)
        //{
        //    output.Write(Text);
        //}

        private bool _includeDateInDisplayName;

	    public bool IncludeDateInDisplayName
	    {
		    get { return _includeDateInDisplayName;}
		    set { _includeDateInDisplayName = value;}
	    }

        private bool _useMusicPlayer;

        public bool UseMusicPlayer
        {
            get { return _useMusicPlayer; }
            set { _useMusicPlayer = value; }
        }


        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Literal llit;
            Panel lpnl;
            Panel lsubpnl;

            if (Editable)
            {
                lpnl = new Panel();
                lpnl.CssClass = CssClass + "ArticleID";

                Label llbl = new Label();
                llbl.Text = "ID: " + Convert.ToString(aArticle.ArticleID);
                llbl.CssClass = lpnl.CssClass + "Label";
                lpnl.Controls.Add(llbl);

                lpnl.Controls.Add(llbl);
                Controls.Add(lpnl);

                lpnl = new Panel();
                lpnl.CssClass = CssClass + "ArticleCode";

                _edArticleCode = new TextBox();
                _edArticleCode.Text = aArticle.ArticleCode;
                _edArticleCode.CssClass = lpnl.CssClass + "TextBox";

                lpnl.Controls.Add(_edArticleCode);
                Controls.Add(lpnl);
            }

            lpnl = new Panel();
            lpnl.CssClass = CssClass + "ArticleName";

            if (Editable)
            {
                _edArticleName = new TextBox();
                _edArticleName.Text = aArticle.ArticleName;
                _edArticleName.CssClass = lpnl.CssClass + "TextBox";
                lpnl.Controls.Add(_edArticleName);
            }
            else
            {
                string lArticleDisplayName;

                if (IncludeDateInDisplayName)
                {
                    lArticleDisplayName = aArticle.ArticleName + " (" + aArticle.LastChanged + ")";
                }
                else
                {
                    lArticleDisplayName = aArticle.ArticleName;
                }

                if (!String.IsNullOrEmpty(NavigateUrl))
                {
                    HyperLink lhref = new HyperLink();
                    lhref.NavigateUrl = NavigateUrl.Replace("%ArticleCode%", aArticle.ArticleCode);
                    lhref.Text = lArticleDisplayName;
                    lpnl.Controls.Add(lhref);
                }
                else
                {
                    llit = new Literal();
                    llit.Text = lArticleDisplayName;
                    lpnl.Controls.Add(llit);
                }
            }

            Controls.Add(lpnl);

            lpnl = new Panel();
            lpnl.CssClass = CssClass + "ArticleUrl";

            if (Editable)
            {
                _edArticleUrl = new TextBox();
                _edArticleUrl.Text = aArticle.Url;
                _edArticleUrl.CssClass = lpnl.CssClass + "TextBox";
                lpnl.Controls.Add(_edArticleUrl);
                Controls.Add(lpnl);
            }
            else
            {
                if (!String.IsNullOrEmpty(aArticle.Url))
                {
                    if (UseMusicPlayer && IsMusicUrl(aArticle.Url))
                    {
                        Panel lpnlPlayer = new Panel();
                        lpnlPlayer.CssClass = CssClass + "ArticleMusicPlayer";

                        Literal lscriptTag = new Literal();
                        lscriptTag.Text = 
                        "<script language=\"JavaScript\" type=\"text/javascript\">" + Environment.NewLine +
                        "<!-- Call external JavaScript file to embed player" + Environment.NewLine +
                        "embedPlayer(\"\",\"" + aArticle.Url + "\"); // title, filename" + Environment.NewLine +
                        "// -->" + Environment.NewLine +
                        "</script>";
                        lpnlPlayer.Controls.Add(lscriptTag);
                        Controls.Add(lpnlPlayer);
                    };

                    HyperLink lhref = new HyperLink();
                    lhref.Text = "Download File (right click or ctrl-click)";
                    lhref.NavigateUrl = aArticle.Url;
                    lpnl.Controls.Add(lhref);
                    Controls.Add(lpnl);
                }
            }

            lpnl = new Panel();
            lpnl.CssClass = CssClass + "ArticleDescription";

            if (Editable)
            {
                lsubpnl = new Panel();
                lsubpnl.CssClass = lpnl.CssClass + "Content";
                lpnl.Controls.Add(lsubpnl);

                _edArticleDescription = new FreeTextBox();
                _edArticleDescription.Text = aArticle.ArticleContent;
                _edArticleDescription.Width = new Unit(100, UnitType.Percentage);
                _edArticleDescription.Height = new Unit(400);

                lsubpnl.Controls.Add(_edArticleDescription);
                Controls.Add(lpnl);
            }
            else
            {
                if (!String.IsNullOrEmpty(aArticle.ArticleContent))
                {
                    llit = new Literal();
                    llit.Text = aArticle.ArticleContent;
                    lpnl.Controls.Add(llit);
                    Controls.Add(lpnl);
                }
            }


            if (Editable)
            {
                lpnl = new Panel();
                lpnl.CssClass = "Submit";

                Button lbutton = new Button();
                lbutton.Text = "Update";
                lbutton.Click += new EventHandler(OnUpdate);
                lbutton.CssClass = lpnl.CssClass + "UpdateButton";

                lpnl.Controls.Add(lbutton);

                lbutton = new Button();
                lbutton.Text = "Delete";
                lbutton.Click += new EventHandler(OnDelete);
                lbutton.OnClientClick = "return confirm('Really delete this article permanently?');";
                lbutton.CssClass = lpnl.CssClass + "DeleteButton";

                lpnl.Controls.Add(lbutton);

                Controls.Add(lpnl);
            }
        }

        private bool IsMusicUrl(string aUrl)
        {
            bool retval = false;

            string[] aUrlSplit = aUrl.Split('.');

            if (aUrlSplit.Length > 1)
            {
                retval = aUrlSplit[aUrlSplit.Length - 1].ToUpper() == "MP3";
            }

            return retval;
        }
    }
}
